#include"ChatServer.h"

void sentpost(int connfd)
{
string tmp;
for(auto c:postmsg)
{
tmp="begin";
if(dealdata(connfd)=="ok")
write(connfd,tmp.c_str(),tmp.size());
tmp=c.getpuser();
if(dealdata(connfd)=="first")
write(connfd,tmp.c_str(),tmp.size());
tmp=c.getptitle();
if(dealdata(connfd)=="second")
write(connfd,tmp.c_str(),tmp.size());
tmp=c.getptime();
if(dealdata(connfd)=="third")
write(connfd,tmp.c_str(),tmp.size());
}
tmp="end";
if(dealdata(connfd)=="ok")
write(connfd,tmp.c_str(),tmp.size());
}
void seedetail(int connfd,string s)
{
//确认收到再发下一个
int i=0;
//for(auto c:s)
//i+=c-'0';
i=2;
string tmp;
tmp=postmsg[i-1].getpuser();
if(dealdata(connfd)=="ok")
write(connfd,tmp.c_str(),tmp.size());
tmp=postmsg[i-1].getptime();
if(dealdata(connfd)=="ok")
write(connfd,tmp.c_str(),tmp.size());
tmp=postmsg[i-1].getptitle();
if(dealdata(connfd)=="ok")
write(connfd,tmp.c_str(),tmp.size());
tmp=postmsg[i-1].getpdata();
if(dealdata(connfd)=="ok")
write(connfd,tmp.c_str(),tmp.size());
}
void dealpost(int connfd,string user)
{
string i,title,data,time;
Post tmp;
string command,sbuff;
while(1)
{
cout<<"111111"<<endl;
sentpost(connfd);
command=dealdata(connfd);
if(command=="seemore")
{sbuff="seemore";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
//get which
if((i=dealdata(connfd))!="error")
{sbuff="ok";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
seedetail(connfd,i);
}
else{
sbuff="error";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));}}
else if(command=="add")
{
sbuff="add";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
title=dealdata(connfd);
sbuff="title";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
data=dealdata(connfd);
sbuff="body";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
time=dealdata(connfd);
sbuff="time";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));

tmp.addpost(user,time,title,data);
postmsg.push_back(tmp);
}
else if(command=="back") {sbuff="back";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));}
else { cout<<"abornomal disconnect"<<endl; break;}
}}
